package com.wh.service.initial;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.mapper.initial.LocationMapper;
import com.wh.mapper.initial.LocationTypeMapper;
import com.wh.mapper.initial.StorehouseMapper;
import com.wh.pojo.Location;
import com.wh.pojo.LocationType;
import com.wh.pojo.Storehouse;
import com.wh.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class LocationServiceImpl implements LocationService{

    @Autowired
    private LocationTypeMapper locationTypeMapper;

    @Autowired
    private StorehouseMapper storehouseMapper;

    @Autowired
    private LocationMapper locationMapper;

    @Override
    public PageResult getLocationList(PageResult pageResult) {
        Page<Location> locationPage = new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        QueryWrapper<Location> queryWrapper = new QueryWrapper<>();
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.like(flag,"name",pageResult.getQuery());
        locationPage = locationMapper.selectPage(locationPage,queryWrapper);
        pageResult.setTotal((long) locationPage.getRecords().size()).setRows(locationPage.getRecords());

        return pageResult;
    }

    @Override
    public void addLocation(Location location) {
        locationMapper.insert(location);
    }

    @Override
    public void updataLocation(Location location) {
        locationMapper.updateById(location);
    }

    @Override
    public void deleteLocation(String name) {
        locationMapper.deleteById(name);
    }

    @Override
    public List<Storehouse> getStorehouse() {
        return storehouseMapper.selectList(null);
    }

    @Override
    public List<LocationType> getType() {
        return locationTypeMapper.selectList(null);
    }

}
